package code.starivan.leetcode;

import java.util.Stack;

/**
 * Created by Ivan on 2015/10/27.
 */
/*

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

        push(x) -- Push element x onto stack.
        pop() -- Removes the element on top of the stack.
        top() -- Get the top element.
        getMin() -- Retrieve the minimum element in the stack.

*/

public class N155_MinStack {

    Stack<Integer> sData=new Stack<>();
    Stack<Integer> sMin=new Stack<>();

    public void push(int x) {
        sData.push(x);
        if(sMin.isEmpty()||getMin()>=x){
            sMin.push(x);
        }
    }

    public void pop() {
        int top=sData.pop();
        if(getMin()==top){
            sMin.pop();
        }
    }

    public int top() {
        return sData.peek();
    }

    public int getMin() {
        return sMin.peek();
    }
}
